/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.samples;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import javax.persistence.*;

/**
 *
 * @author Glauco
 */
@Entity
@IdClass(EnderecoAdicional.ID.class)
public class EnderecoAdicional implements Serializable{

    @Transient
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }
    public static final String PROP_TIPOENDERECO = "tipoEndereco";
    public static final String PROP_ID = "id";
    public static final String PROP_ENDERECO = "endereco";
    public static final String PROP_PESSOA = "pessoa";
    
    @Id
    @ManyToOne
    private Pessoa pessoa;

    @Id
    @Basic
    private Long id;
    
    @Basic
    @Enumerated(value=EnumType.STRING)
    private TipoEndereco tipoEndereco;

    @Embedded
    private Endereco endereco;

    public Endereco getEndereco() {
        return endereco;
    }

    public void setEndereco(Endereco endereco) {
        Endereco oldEndereco = this.endereco;
        this.endereco = endereco;
        propertyChangeSupport.firePropertyChange(PROP_ENDERECO, oldEndereco, endereco);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        Long oldId = this.id;
        this.id = id;
        propertyChangeSupport.firePropertyChange(PROP_ID, oldId, id);
    }

    public TipoEndereco getTipoEndereco() {
        return tipoEndereco;
    }

    public void setTipoEndereco(TipoEndereco tipoEndereco) {
        TipoEndereco oldTipoEndereco = this.tipoEndereco;
        this.tipoEndereco = tipoEndereco;
        propertyChangeSupport.firePropertyChange(PROP_TIPOENDERECO, oldTipoEndereco, tipoEndereco);
    }

    public Pessoa getPessoa() {
        return pessoa;
    }

    public void setPessoa(Pessoa pessoa) {
        Pessoa old = this.pessoa;
        this.pessoa = pessoa;
        propertyChangeSupport.firePropertyChange(PROP_PESSOA,old,pessoa);
    }

    public static class ID implements Serializable{

        public Pessoa pessoa;
        public Long id;

        public ID() {
        }

        public ID(Pessoa pessoa, Long id) {
            this.pessoa = pessoa;
            this.id = id;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final ID other = (ID) obj;
            if (this.pessoa != other.pessoa &&
                    (this.pessoa == null || !this.pessoa.equals(other.pessoa))) {
                return false;
            }
            if (this.id != other.id &&
                    (this.id == null || !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }

        @Override
        public int hashCode() {
            return this.id.hashCode() + this.pessoa.hashCode();
        }


    }

}
